package com.hiultra.archive.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.mapper.*;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.uuid.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.service.IArchiveStocktakingService;

/**
 * 盘点数据Service业务层处理
 *
 * @author paidaxing
 * @date 2024-07-31
 */
@Service
public class ArchiveStocktakingServiceImpl implements IArchiveStocktakingService {
    // 档案室编号
    @Value("${roomNo}")
    private String roomNo;
    @Autowired
    private ArchiveStocktakingMapper archiveStocktakingMapper;
    @Autowired
    private ArchiveStocktakingBoxMapper archiveStocktakingBoxMapper;
    @Autowired
    private ArchiveStocktakingTaskMapper archiveStocktakingTaskMapper;

    /**
     * 查询盘点数据
     *
     * @param id 盘点数据主键
     * @return 盘点数据
     */
    @Override
    public ArchiveStocktaking selectArchiveStocktakingById(String id) {
        return archiveStocktakingMapper.selectArchiveStocktakingById(id);
    }

    /**
     * 查询盘点数据列表
     *
     * @param archiveStocktaking 盘点数据
     * @return 盘点数据
     */
    @Override
    public List<ArchiveStocktaking> selectArchiveStocktakingList(ArchiveStocktaking archiveStocktaking) {
        return archiveStocktakingMapper.selectArchiveStocktakingList(archiveStocktaking);
    }

    /**
     * 查询盘点数据列表
     *
     * @param archiveStocktaking 盘点数据
     * @return 盘点数据集合
     */
    @Override
    public List<ArchiveStocktaking> selectArchiveStocktakingListReport(ArchiveStocktaking archiveStocktaking) {
        return archiveStocktakingMapper.selectArchiveStocktakingListReport(archiveStocktaking);
    }

    /**
     * 新增盘点数据
     *
     * @param archiveStocktaking 盘点数据
     * @return 结果
     */
    @Override
    public int insertArchiveStocktaking(ArchiveStocktaking archiveStocktaking) {
        return archiveStocktakingMapper.insertArchiveStocktaking(archiveStocktaking);
    }

    /**
     * 修改盘点数据
     *
     * @param archiveStocktaking 盘点数据
     * @return 结果
     */
    @Override
    public int updateArchiveStocktaking(ArchiveStocktaking archiveStocktaking) {
        return archiveStocktakingMapper.updateArchiveStocktaking(archiveStocktaking);
    }

    /**
     * 批量删除盘点数据
     *
     * @param ids 需要删除的盘点数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStocktakingByIds(String[] ids) {
        return archiveStocktakingMapper.deleteArchiveStocktakingByIds(ids);
    }

    /**
     * 删除盘点数据信息
     *
     * @param id 盘点数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStocktakingById(String id) {
        return archiveStocktakingMapper.deleteArchiveStocktakingById(id);
    }

    /**
     * 批量新增
     *
     * @param archiveStocktakingList 盘点数据
     * @return 结果
     */
    @Override
    public int batchinsertArchiveStocktaking(List<ArchiveStocktaking> archiveStocktakingList) {
        if (ObjectUtil.isEmpty(archiveStocktakingList)) {
            return 0;
        }
        return archiveStocktakingMapper.batchinsertArchiveStocktaking(archiveStocktakingList);
    }

    /**
     * 根据档案盒boxno获取盘点档案列表详细信息
     */
    @Override
    public List<ArchiveStocktaking> selectArchiveListByTaskBoxId(String taskBoxId) {
        return archiveStocktakingMapper.selectArchiveListByTaskBoxId(taskBoxId);
    }

    /**
     * 修改盘点单-盘点档案
     *
     * @param archiveStocktaking
     * @return
     */
    @Override
    public int editArchiveStocktaking(ArchiveStocktaking archiveStocktaking) {
        if (archiveStocktaking.getId().equals("add")) {
            archiveStocktaking.setId(UUIDUtil.getUUID32());
            return insertArchiveStocktaking(archiveStocktaking);
        } else {
            return updateArchiveStocktaking(archiveStocktaking);
        }
//        //根据taskid 查询是否都有盘点结果，如果都有则判 定为盘点结束
//        ArchiveStocktakingBox archiveStocktakingBox1 = new ArchiveStocktakingBox();
//        archiveStocktakingBox1.setTaskId(archiveStocktaking.getTaskId());
//        archiveStocktaking.setTaskId(archiveStocktaking.getTaskId());
//        List<ArchiveStocktakingBox> archiveStocktakingBoxList = archiveStocktakingBoxMapper.selectArchiveStocktakingBoxList(archiveStocktakingBox1);
//        List<ArchiveStocktaking> archiveStocktakingList = archiveStocktakingMapper.selectArchiveStocktakingList(archiveStocktaking);
//        //判断是否盘点完成
//        int inventoryResultNum = 0;
//        for (ArchiveStocktakingBox archiveStocktakingBox2 : archiveStocktakingBoxList) {
//            if (ObjectUtil.isEmpty(archiveStocktakingBox2.getInventoryResult())) {
//                inventoryResultNum++;
//                break;
//            }
//        }
//        for (ArchiveStocktaking archiveStocking : archiveStocktakingList) {
//            if (ObjectUtil.isEmpty(archiveStocking.getInventoryResult())) {
//                inventoryResultNum++;
//                break;
//            }
//        }
//        if (inventoryResultNum <= 0) {
//            archiveStocktakingTaskMapper.updateArchiveStocktakingTask(new ArchiveStocktakingTask() {{
//                setTaskId(archiveStocktaking.getTaskId());
//                setInventoryTime(DateUtils.getNowDate());
//                setStatus(BusinessConstants.END_INVENTORY_ORDER_STATUS);
//            }});
//        }

    }
}
